/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kimstore.gui.repair;

import com.kimstore.bean.Item;
import com.kimstore.bean.ItemStatus;
import com.kimstore.bean.RepairDetail;
import com.kimstore.factory.DaoFactory;
import com.kimstore.gui.sales.SelectReplacementItemController;
import com.kimstore.main.controller.RepairTabController;
import java.util.Date;

/**
 *
 * @author Administrator
 */
public class ViewItemForRepairController {

    private RepairTabController parentController;
    private ViewItemForRepairWindow window;
    private Item item;
    private Item replacementItem;

    public ViewItemForRepairController(RepairTabController parentController, String SKU) {
        this.parentController = parentController;
        this.item = DaoFactory.getInstance().getItemDao().findBySerial(SKU);
    }

    public void displayWindow() {
        this.window = new ViewItemForRepairWindow(this);
        window.populateWindow(item, DaoFactory.getInstance().getStaffDao().findAll());
        window.showWindow();
    }

    public void fromWindow(Item item) {
        replacementItem.setPurchaseDetail(DaoFactory.getInstance().getPurchaseDetailDao().findById(item.getPurchaseDetail().getId()));

        if (item.getRepairDetail().getReplacement() == null) {
            item.setPurchaseDetail(null);
        } else {
            item.getRepairDetail().getReplacement().setPurchaseDetail(null);
        }

        item.getRepairDetail().setReplacement(replacementItem);

        DaoFactory.getInstance().getItemDao().saveOrUpdate(replacementItem);
        DaoFactory.getInstance().getItemDao().saveOrUpdate(item);
        parentController.refreshTable();
    }

    public void fromWindow(Item item, String verdict) {
        if (verdict.equals(RepairDetail.REPLACEMENT)) {
            if (item.getRepairDetail().getReplacement() == null) {
                replacementItem.setPurchaseDetail(DaoFactory.getInstance().getPurchaseDetailDao().findById(item.getPurchaseDetail().getId()));
                item.setPurchaseDetail(null);
            } else {
                replacementItem.setPurchaseDetail(DaoFactory.getInstance().getPurchaseDetailDao().findById(item.getRepairDetail().getReplacement().getPurchaseDetail().getId()));
                item.getRepairDetail().getReplacement().setPurchaseDetail(null);
                DaoFactory.getInstance().getItemDao().saveOrUpdate(item.getRepairDetail().getReplacement());
            }

            item.getRepairDetail().setReplacement(replacementItem);

            DaoFactory.getInstance().getItemDao().saveOrUpdate(replacementItem);

        }


        DaoFactory.getInstance().getItemDao().saveOrUpdate(item);

        parentController.refreshTable();
    }

    public void createSelectReplacementItemController() {
        SelectReplacementItemController controller = new SelectReplacementItemController(this);
        controller.displayWindow();
    }

    public void createSelectReplacementItemControllerCallback(Item item) {
        this.replacementItem = item;

        if (this.item.getPurchaseDetail() != null) {
            window.addAdditionalCost(this.item.getPurchaseDetail().getPriceSold());

        }
        window.displayItemForRepair(item);
    }
}
